﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using DevExpress.Web.ASPxGridView;
using DAL;
using Frame.Global;
using DevExpress.Web.ASPxEditors;
using System.Text;
public partial class ShowProject : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Frame.Global.GlobalFunctions.InitComboBoxColumn(gvPro.Columns["t_Type_ID"], "TypeInfo", "Type_Name", "Type_ID");
            Frame.Global.GlobalFunctions.InitComboBoxColumn(gvPro.Columns["t_AppNum"], "Approal", "Approal_ID", "Approal_ID");
            Frame.Global.GlobalFunctions.InitComboBoxColumn(gvPro.Columns["t_Community_ID"], "Community", "Community_Name", "Community_ID");
            Session["D_ID"] = CMSUser.Current.UserTypeName;
            CmbDisBind();
            this.CmbOr.Text = "";
        }

    }

    //绑定城区信息
    private void CmbDisBind()
    {
        //判断用户是否已经登录
        try
        {
            if (!CMSUser.Current.IsLogin)
            {
                Response.Redirect("~/Login.aspx");
            }
        }
        catch (Exception)
        {
            Response.Redirect("~/Login.aspx");
        }
        //获取当前登录的用户
        string UserName = CMSUser.Current.Name;
        if (!UserName.Equals("Administrator"))
        {
            CmbDis.DataSourceID = "SDSDis";
            CmbDis.DataBind();
            CmbDis.ReadOnly = true;
        }
        if (UserName.Equals("Administrator"))
        {
            CmbDis.DataSourceID = "SDSDisAll";
            CmbDis.DataBind();
            this.CmbDis.Text = "";
        }
    }


    //用户登录查看对应社区的项目(LINQ数据源)
    protected void LinqServerModeDataSource1_Selecting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceSelectEventArgs e)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        //获取当前登录的用户
        string username = CMSUser.Current.Name;
        //查询用户对应的社区
        string CommunityName = Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select Communities from Users where UserName='" + username + "'").ToString();
        string strSql;
        strSql = "select * from ProjectDetails where Substring(Community_ID,1, 2)='" + CommunityName + "'";
        DataTable dt = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strSql);
        if (dt.Rows.Count > 0)
        {

            e.QueryableSource = dc.ProjectDetails.Where(p => p.Community_ID.Substring(0, 2) == CommunityName);
        }
        else
        {
            if (!username.Equals("Administrator"))
            {
                e.QueryableSource = dc.ProjectDetails.Where(p => p.Community_ID == "xxx");
            }

        }
    }

    //操作时间为当前时间
    protected void gvPro_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        e.NewValues["OperateDate"] = DateTime.Now.ToShortDateString();
    }

    //截取过长信息显示
    public string CutStr(string str)
    {
        if (str.Length > 50)
        {
            str = str.Substring(0, 49) + "…";
        }
        return str;
    }

    //按条件查询
    protected void CBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        string SqlFormat = @" SELECT TypeInfo.Type_ID,
                                    ProjectDetails.Project_ID,
                                    ProjectDetails.Project_Name,
                                    ProjectDetails.Project_Development,
                                    ProjectDetails.Project_BasicInfo,
                                    ProjectDetails.Project_Address,
                                    ProjectDetails.Project_Scope,
                                    ProjectDetails.ServeObiect,
                                    ProjectDetails.ServerPersontime,
                                    ProjectDetails.ContactWay,
                                    ProjectDetails.other,
                                    ProjectDetails.Project_CitySubsidy,
                                    ProjectDetails.Project_DistrictFund,
                                    ProjectDetails.Project_FundsTotal,
                                    ProjectDetails.Community_ID,
                                    ProjectDetails.ApprovalNumber,
                                    Approal.Approal_ID,
                                    Approal.Approal_Date,
                                    ProjectDetails.FinishDate,
                                    ProjectDetails.Project_Performance,
                                    ProjectDetails.OperateDate,
                                    ProjectDetails.ReportDate,
                                    Project_Performance
                                    FROM ProjectDetails
                                    INNER JOIN TypeInfo
                                    ON ProjectDetails.Type_ID=TypeInfo.Type_ID
                                    INNER JOIN Approal
                                    ON Projectdetails.ApprovalNumber=Approal.ID
                                    WHERE {0} 1=1
                                    ORDER BY ProjectDetails.FinishDate
                                ";
        StringBuilder strb = new StringBuilder();
        List<SqlParameter> Params = new List<SqlParameter>();
        if (!string.IsNullOrEmpty(CmbDis.Text.Trim()))
        {
            strb.Append("ProjectDetails.Community_ID like ''+@D_ID+'%'  AND ");
            Params.Add(new SqlParameter("@D_ID", CmbDis.Value.ToString()));
        }
        if (!string.IsNullOrEmpty(txtDate.Text.Trim()))
        {
            strb.Append("Approal.Approal_Date like ''+@F_D+'%' AND ");
            Params.Add(new SqlParameter("@F_D", txtDate.Text.Trim()));
        }
        if (!string.IsNullOrEmpty(txtName.Text.Trim()))
        {
            strb.Append("ProjectDetails.Project_Name like '%'+@P_Name+'%'  AND ");
            Params.Add(new SqlParameter("@P_Name", txtName.Text.ToString()));
        }
        if (CmbOr.Text == "完成")
        {
            strb.Append("Convert(Varchar(10),ProjectDetails.FinishDate,111) is not null AND ");
            Params.Add(new SqlParameter("", ""));
        } if (CmbOr.Text == "未完成")
        {
            strb.Append("Convert(Varchar(10),ProjectDetails.FinishDate,111) is null AND ");
            Params.Add(new SqlParameter("", ""));
        }
        Session["sqlstr"] = string.Format(SqlFormat, strb.ToString());
        Session["sqlParams"] = Params.ToArray();
        this.gvPro.DataSourceID = "";
        gvPro.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(string.Format(SqlFormat, strb.ToString()), Params.ToArray());
        gvPro.DataBind();
    }

    //用户登录查看对应社区的项目(SQL数据源)
    protected void SDSAll_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        //获取当前登录的用户
        string username = CMSUser.Current.Name;
        //查询用户对应的社区
        string CommunityName = Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select Communities from Users where UserName='" + username + "'").ToString();
        string strSql;
        strSql = "select * from ProjectDetails where Substring(Community_ID,1, 2)='" + CommunityName + "'";
        DataTable dt = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strSql);
        if (dt.Rows.Count > 0)
        {
            string strSqls = @"SELECT TypeInfo.Type_ID,
                                    ProjectDetails.Project_ID,
                                    ProjectDetails.Project_Name,
                                    ProjectDetails.Project_Development,
                                    ProjectDetails.Project_BasicInfo,
                                    ProjectDetails.Project_Address,
                                    ProjectDetails.Project_Scope,
                                    ProjectDetails.ServeObiect,
                                    ProjectDetails.ServerPersontime,
                                    ProjectDetails.ContactWay,
                                    ProjectDetails.other,
                                    ProjectDetails.Project_CitySubsidy,
                                    ProjectDetails.Project_DistrictFund,
                                    ProjectDetails.Project_FundsTotal,
                                    ProjectDetails.Community_ID,
                                    ProjectDetails.ApprovalNumber,
                                    Approal.Approal_ID,
                                    ProjectDetails.FinishDate,
                                    ProjectDetails.Project_Performance,
                                    ProjectDetails.OperateDate,
                                    ProjectDetails.ReportDate,
                                    Project_Performance
                                    FROM ProjectDetails
                                    INNER JOIN TypeInfo
                                    ON ProjectDetails.Type_ID=TypeInfo.Type_ID
                                    INNER JOIN Approal
                                    ON Projectdetails.ApprovalNumber=Approal.ID
                                    WHERE Substring(ProjectDetails.Community_ID,1, 2)='" + CommunityName + "'  order by Approal.Approal_Date asc";
            e.Command.CommandText = strSqls;
        }
        else
        {
            if (!username.Equals("Administrator"))
            {
                string id = "xxx";
                e.Command.CommandText = @"SELECT TypeInfo.Type_ID,
                                    ProjectDetails.Project_ID,
                                    ProjectDetails.Project_Name,
                                    ProjectDetails.Project_Development,
                                    ProjectDetails.Project_BasicInfo,
                                    ProjectDetails.Project_Address,
                                    ProjectDetails.Project_Scope,
                                    ProjectDetails.ServeObiect,
                                    ProjectDetails.ServerPersontime,
                                    ProjectDetails.ContactWay,
                                    ProjectDetails.other,
                                    ProjectDetails.Project_CitySubsidy,
                                    ProjectDetails.Project_DistrictFund,
                                    ProjectDetails.Project_FundsTotal,
                                    ProjectDetails.Community_ID,
                                    ProjectDetails.ApprovalNumber,
                                    Approal.Approal_ID,
                                    ProjectDetails.FinishDate,
                                     ProjectDetails.Project_Performance,
                                    ProjectDetails.OperateDate,
                                    ProjectDetails.ReportDate,
                                    Project_Performance
                                    FROM ProjectDetails
                                    INNER JOIN TypeInfo
                                    ON ProjectDetails.Type_ID=TypeInfo.Type_ID
                                    INNER JOIN Approal
                                    ON Projectdetails.ApprovalNumber=Approal.ID 
                                    WHERE  Community_ID='" + id + "'";
            }
        }
    }
    protected void gvPro_RowCommand(object sender, ASPxGridViewRowCommandEventArgs e)
    {
        using (DataClassesDataContext dc = new DataClassesDataContext())
        {
            string P_ID = e.KeyValue.ToString();
            ProjectDetails info = dc.ProjectDetails.FirstOrDefault(p => p.Project_ID == P_ID);
            dc.ProjectDetails.DeleteOnSubmit(info);
            dc.SubmitChanges();
            Frame.Global.GlobalFunctions.AlertToUrl("删除成功！", "ShowProject.aspx");
        }
    }
    //清空检索条件
    protected void btnCl_Click(object sender, EventArgs e)
    {
        this.CmbDis.Text = "";
        this.txtName.Text = "";
        this.txtDate.Text = "";
        this.gvPro.DataSourceID = "SDSAll";
    }
}